package com.jeequan.jeepay.service.impl;

//package .service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeequan.jeepay.core.entity.PaymentOrder;
import com.jeequan.jeepay.service.PaymentOrderService;
import com.jeequan.jeepay.service.mapper.PaymentOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
//import .entity.PaymentOrder;
//import .mapper.PaymentOrderMapper;
//import .service.PaymentOrderService;
 /**
 * 代付订单表;(payment_order)表服务实现类
 * @author : wondream322@gmail.com
 * @date : 2023-8-19
 */
@Service
public class PaymentOrderServiceImpl implements PaymentOrderService {
    @Autowired
    private PaymentOrderMapper paymentOrderMapper;
    
    /** 
     * 通过ID查询单条数据 
     *
     * @param id 主键
     * @return 实例对象
     */
    public PaymentOrder queryById(Integer id){
        return paymentOrderMapper.selectById(id);
    }
    
    /**
     * 分页查询
     *
     * @param paymentOrder 筛选条件
     * @param current 当前页码
     * @param size  每页大小
     * @return
     */
    public Page<PaymentOrder> paginQuery(PaymentOrder paymentOrder, long current, long size){
        //1. 构建动态查询条件
        LambdaQueryWrapper<PaymentOrder> queryWrapper = new LambdaQueryWrapper<>();
        if(paymentOrder.getMemberId()!=null){
            queryWrapper.eq(PaymentOrder::getMemberId, paymentOrder.getMemberId());
        }
        if(paymentOrder.getBalanceType()!=null){
            queryWrapper.eq(PaymentOrder::getBalanceType, paymentOrder.getBalanceType());
        }
        if(paymentOrder.getState()!=null){
            queryWrapper.eq(PaymentOrder::getState, paymentOrder.getState());
        }
        if(StrUtil.isNotBlank(paymentOrder.getOrderId())){
            queryWrapper.eq(PaymentOrder::getOrderId, paymentOrder.getOrderId());
        }
        if(StrUtil.isNotBlank(paymentOrder.getOutTradeNo())){
            queryWrapper.eq(PaymentOrder::getOutTradeNo, paymentOrder.getOutTradeNo());
        }
        if(StrUtil.isNotBlank(paymentOrder.getChannelOrderId())){
            queryWrapper.eq(PaymentOrder::getChannelOrderId, paymentOrder.getChannelOrderId());
        }
        if(StrUtil.isNotBlank(paymentOrder.getNotifyUrl())){
            queryWrapper.eq(PaymentOrder::getNotifyUrl, paymentOrder.getNotifyUrl());
        }
        if(StrUtil.isNotBlank(paymentOrder.getBankCardNo())){
            queryWrapper.eq(PaymentOrder::getBankCardNo, paymentOrder.getBankCardNo());
        }
        if(StrUtil.isNotBlank(paymentOrder.getBankAccount())){
            queryWrapper.eq(PaymentOrder::getBankAccount, paymentOrder.getBankAccount());
        }
        if(StrUtil.isNotBlank(paymentOrder.getBankCode())){
            queryWrapper.eq(PaymentOrder::getBankCode, paymentOrder.getBankCode());
        }
        if(StrUtil.isNotBlank(paymentOrder.getIfsc())){
            queryWrapper.eq(PaymentOrder::getIfsc, paymentOrder.getIfsc());
        }
        if(StrUtil.isNotBlank(paymentOrder.getMobile())){
            queryWrapper.eq(PaymentOrder::getMobile, paymentOrder.getMobile());
        }
        if(StrUtil.isNotBlank(paymentOrder.getEmail())){
            queryWrapper.eq(PaymentOrder::getEmail, paymentOrder.getEmail());
        }
        if(StrUtil.isNotBlank(paymentOrder.getExtend())){
            queryWrapper.eq(PaymentOrder::getExtend, paymentOrder.getExtend());
        }
        if(StrUtil.isNotBlank(paymentOrder.getDfCode())){
            queryWrapper.eq(PaymentOrder::getDfCode, paymentOrder.getDfCode());
        }
        if(StrUtil.isNotBlank(paymentOrder.getDfName())){
            queryWrapper.eq(PaymentOrder::getDfName, paymentOrder.getDfName());
        }
        if(StrUtil.isNotBlank(paymentOrder.getChannelMchId())){
            queryWrapper.eq(PaymentOrder::getChannelMchId, paymentOrder.getChannelMchId());
        }
        if(StrUtil.isNotBlank(paymentOrder.getRemark())){
            queryWrapper.eq(PaymentOrder::getRemark, paymentOrder.getRemark());
        }
        //2. 执行分页查询
        Page<PaymentOrder> pagin = new Page<>(current , size , true);
        IPage<PaymentOrder> selectResult = paymentOrderMapper.selectByPage(pagin , queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }
    
    /** 
     * 新增数据
     *
     * @param paymentOrder 实例对象
     * @return 实例对象
     */
    public PaymentOrder insert(PaymentOrder paymentOrder){
        int insertNum = paymentOrderMapper.insert(paymentOrder);
        if (insertNum>0) {
            return paymentOrder;
        }
        return null;
    }
    
    /** 
     * 更新数据
     *
     * @param paymentOrder 实例对象
     * @return 实例对象
     */
    public PaymentOrder update(PaymentOrder paymentOrder){
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<PaymentOrder> chainWrapper = new LambdaUpdateChainWrapper<PaymentOrder>(paymentOrderMapper);
        if(StrUtil.isNotBlank(paymentOrder.getOrderId())){
            chainWrapper.set(PaymentOrder::getOrderId, paymentOrder.getOrderId());
        }
        if(StrUtil.isNotBlank(paymentOrder.getOutTradeNo())){
            chainWrapper.set(PaymentOrder::getOutTradeNo, paymentOrder.getOutTradeNo());
        }
        if(StrUtil.isNotBlank(paymentOrder.getChannelOrderId())){
            chainWrapper.set(PaymentOrder::getChannelOrderId, paymentOrder.getChannelOrderId());
        }

        if(StrUtil.isNotBlank(paymentOrder.getNotifyUrl())){
            chainWrapper.set(PaymentOrder::getNotifyUrl, paymentOrder.getNotifyUrl());
        }
        if(StrUtil.isNotBlank(paymentOrder.getBankCardNo())){
            chainWrapper.set(PaymentOrder::getBankCardNo, paymentOrder.getBankCardNo());
        }
        if(StrUtil.isNotBlank(paymentOrder.getBankAccount())){
            chainWrapper.set(PaymentOrder::getBankAccount, paymentOrder.getBankAccount());
        }
        if(StrUtil.isNotBlank(paymentOrder.getBankCode())){
            chainWrapper.set(PaymentOrder::getBankCode, paymentOrder.getBankCode());
        }
        if(StrUtil.isNotBlank(paymentOrder.getIfsc())){
            chainWrapper.set(PaymentOrder::getIfsc, paymentOrder.getIfsc());
        }
        if(StrUtil.isNotBlank(paymentOrder.getMobile())){
            chainWrapper.set(PaymentOrder::getMobile, paymentOrder.getMobile());
        }
        if(StrUtil.isNotBlank(paymentOrder.getEmail())){
            chainWrapper.set(PaymentOrder::getEmail, paymentOrder.getEmail());
        }
        if(StrUtil.isNotBlank(paymentOrder.getExtend())){
            chainWrapper.set(PaymentOrder::getExtend, paymentOrder.getExtend());
        }
        if(StrUtil.isNotBlank(paymentOrder.getDfCode())){
            chainWrapper.set(PaymentOrder::getDfCode, paymentOrder.getDfCode());
        }
        if(StrUtil.isNotBlank(paymentOrder.getDfName())){
            chainWrapper.set(PaymentOrder::getDfName, paymentOrder.getDfName());
        }
        if(StrUtil.isNotBlank(paymentOrder.getChannelMchId())){
            chainWrapper.set(PaymentOrder::getChannelMchId, paymentOrder.getChannelMchId());
        }
        if(StrUtil.isNotBlank(paymentOrder.getRemark())){
            chainWrapper.set(PaymentOrder::getRemark, paymentOrder.getRemark());
        }
        //2. 设置主键，并更新
        chainWrapper.eq(PaymentOrder::getId, paymentOrder.getId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if(ret){
            return queryById(paymentOrder.getId());
        }else{
            return paymentOrder;
        }
    }
    
    /** 
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public boolean deleteById(Integer id){
        int total = paymentOrderMapper.deleteById(id);
        return total > 0;
    }
}